package com.itheima.zxingdemo.slice;

import com.itheima.zxingdemo.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.app.Context;

public class MainDemoAbilitySlice extends AbilitySlice implements Component.ClickedListener {

    private Button mBtn1;
    private TextField mEt;
    private Button mBtn2;
    private Image mImage;
    private final static int REQ_CODE = 1028;
    private Context mContext;
    private Text mTvResult;
    private Image mImageCallback;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main_demo);
        initView();
        mContext = this;
    }
    private void initView() {
        mBtn1 = (Button) findComponentById(ResourceTable.Id_btn1);
        mBtn1.setClickedListener(this);
        mEt = (TextField) findComponentById(ResourceTable.Id_et);
        mBtn2 = (Button) findComponentById(ResourceTable.Id_btn2);
        mBtn2.setClickedListener(this);
        mImage = (Image) findComponentById(ResourceTable.Id_image);
        mImage.setClickedListener(this);
        mTvResult = (Text) findComponentById(ResourceTable.Id_tv_result);
        mTvResult.setClickedListener(this);
        mImageCallback = (Image) findComponentById(ResourceTable.Id_image_callback);
        mImageCallback.setClickedListener(this);
    }
    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onClick(Component component) {
//        switch (component.getId()) {
//            case ResourceTable.Id_btn1:
////                startActivity(new Intent(MainActivity.this, CaptureActivity.class));
//                Intent intent = new Intent(mContext, CaptureActivity.class);
//                startActivityForResult(intent, REQ_CODE);
//                break;
//            case ResourceTable.Id_btn2:
//                mImage.setVisibility(Component.VISIBLE);
//                //隐藏扫码结果view
//                mImageCallback.setVisibility(Component.HIDE);
//                mTvResult.setVisibility(Component.HIDE);
//
//                String content = mEt.getText().toString().trim();
//                Bitmap bitmap = null;
//                try {
//                    bitmap = BitmapUtils.create2DCode(content);//根据内容生成二维码
//                    mTvResult.setVisibility(Component.HIDE);
//                    mImage.setImageBitmap(bitmap);
//                } catch (WriterException e) {
//                    e.printStackTrace();
//                }
//                break;
//        }
    }
}
